package com.vogtec.fota.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vogtec.fota.config.Config;
import com.vogtec.fota.dao.FirmWareDao;
import com.vogtec.fota.dao.model.FirmWare;
import com.vogtec.fota.entity.FirmWare.*;
import com.vogtec.fota.feign.UpgradeClient;
import com.vogtec.fota.service.FirmWareService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wei on 2017/7/3.
 */
@Service
public class FirmWareServiceImpl implements FirmWareService{

    private static final Logger log = LoggerFactory.getLogger(FirmWareServiceImpl.class);


    @Autowired
    private FirmWareDao firmWareDao;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private UpgradeClient upgradeClient;
    @Autowired
    private Config Config;

    @Override
    public AddFirmWareResult addFirmWare() {
        AddFirmWareResult afwr = new AddFirmWareResult();
        try{
            String token = upgradeClient.getToken("upgrade-service", "http://"+
                    Config.getAddress() + ":" + Config.getPort() + "/sharingbike/v1.0/upgrade/fota/firmware/7niunotify");
            afwr.setUptoken(token);

        }catch (Exception e){
            log.error("",e);
        }
        return afwr;
    }

    @Override
    public FWQueryResult fwQuery(Integer id) {
        FWQueryResult fwqr = new FWQueryResult();
        FirmWare firmWare = firmWareDao.selectFWById(id);
        if(firmWare != null){
            fwqr.setResult("SUCCESS");
            fwqr.setFirmWare(firmWare);
            log.debug("firmWare is : " + firmWare.toString());
        }else {
            fwqr.setResult("FAIL");
            fwqr.setDescription("服务器出错");
            log.error("id [" + id + "]'s firmWare is null!!!");
        }
        return fwqr;
    }

    @Override
    public FWQueryAllResult fwQueryAll(Integer pageNo, Integer pageSize) {
        FWQueryAllResult fwqar = new FWQueryAllResult();

        int total = firmWareDao.selectAllFWTotal();

        //pageSize无参时，返回全部数据
        if(pageSize.equals(0)){
            List<FirmWare> fws = firmWareDao.select();
            log.info(fws.toString());

            if(fws != null){
                fwqar.setResult("SUCCESS");
                fwqar.setFwList(fws);
                fwqar.setTatol(total);
                log.debug("fwList's attentionTotal is : " + fws.size());
            }else {
                fwqar.setResult("FAIL");
                fwqar.setDescription("服务器出错");
                log.error("something wrong");
            }
            return fwqar;
        }

        //pageSize有参数时，执行分页查询
        Page<FirmWare> fwList = PageHelper.startPage(pageNo, pageSize);
//        firmWareDao.selectAllFW();
        firmWareDao.select();
        if(fwList != null){
            fwqar.setResult("SUCCESS");
            fwqar.setFwList(fwList);
            fwqar.setTatol(total);
            log.debug("fwList's attentionTotal is : " + fwList.size());
        }else {
            fwqar.setResult("FAIL");
            fwqar.setDescription("服务器出错");
            log.error("something wrong");
        }
        return fwqar;
    }

    @Override
    public DeleteFWResult deleteFW(Integer id) {
        DeleteFWResult dfwr = new DeleteFWResult();
        int value = firmWareDao.delete(id);
        switch (value){
            case 1:
                dfwr.setResult("SUCCESS");
                log.info("delete success");
                break;
            case 0:
                dfwr.setResult("FAIL");
                dfwr.setDescription("服务器出错");
                log.info("delete fail");
                //失败处理
                return dfwr;
        }
        //成功处理
        return dfwr;
    }

    @Override
    public void notifyToken(AddFirmWare afw) {

        log.debug("******in notifyToken******");

        try{
            log.debug(afw.toString());

            if(afw.getFirmWareUrl().contains("oqg31s7kl.bkt.clouddn.com")){
                String url = afw.getFirmWareUrl().replace("oqg31s7kl.bkt.clouddn.com","obj.vogtec.com");
                afw.setFirmWareUrl(url);
            }

            String json = objectMapper.writeValueAsString(afw);
            FirmWare firmWare = objectMapper.readValue(json, FirmWare.class);

            log.debug(firmWare.toString());

            int value = firmWareDao.insert(firmWare);
            if(value == 1)
                log.info(">>== insert firmWare SUCCESS!!! firmWareName : " + firmWare.getFirmWareName());
            else
                log.error(">>== insert firmWare FAIL !!! firmWareName : " + firmWare.getFirmWareName());
        }catch (Exception e){
            log.error("",e);
        }
    }
}